Skip to content

[compiler-rt][profile] Add COMPILER_RT_BUILD_PROFILE_ROCM option - #200127

Merged
guy-david merged 1 commit into
mainfrom
users/guy-david/profile-rocm-option
May 28, 2026
Merged

[compiler-rt][profile] Add COMPILER_RT_BUILD_PROFILE_ROCM option#200127
guy-david merged 1 commit into
mainfrom
users/guy-david/profile-rocm-option

Conversation

@guy-david

Copy link
Copy Markdown
Contributor

InstrProfilingPlatformROCm.cpp (added in 5db1364 or #177665) is currently built into every non-baremetal libclang_rt.profile, but not all hosts support it (e.g. it fails to link on Darwin, where __llvm_write_custom_profile is unavailable).

Add a COMPILER_RT_BUILD_PROFILE_ROCM CMake option (defaulting off on Apple, on elsewhere) to allow that host to opt out.

InstrProfilingPlatformROCm.cpp (added in 5db1364) is currently built
into every non-baremetal libclang_rt.profile, but not all hosts support it
(e.g. it fails to link on Darwin, where __llvm_write_custom_profile is
unavailable).

Add a COMPILER_RT_BUILD_PROFILE_ROCM CMake option (defaulting off on Apple,
on elsewhere) to allow that host to opt out.
@guy-david
guy-david requested review from jhuber6 and yxsamliu May 28, 2026 08:17
@llvmorg-github-actions llvmorg-github-actions Bot added compiler-rt PGO Profile Guided Optimizations labels May 28, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-pgo

Author: Guy David (guy-david)

Changes

InstrProfilingPlatformROCm.cpp (added in 5db1364 or #177665) is currently built into every non-baremetal libclang_rt.profile, but not all hosts support it (e.g. it fails to link on Darwin, where __llvm_write_custom_profile is unavailable).

Add a COMPILER_RT_BUILD_PROFILE_ROCM CMake option (defaulting off on Apple, on elsewhere) to allow that host to opt out.


Full diff: https://github.com/llvm/llvm-project/pull/200127.diff

3 Files Affected:

  • (modified) compiler-rt/CMakeLists.txt (+9)
  • (modified) compiler-rt/lib/profile/CMakeLists.txt (+9-1)
  • (modified) compiler-rt/lib/profile/InstrProfilingFile.c (+4)
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index e88321d822f84..39034fd9ba67d 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -322,6 +322,15 @@ option(COMPILER_RT_USE_ATOMIC_LIBRARY "Use compiler-rt atomic instead of libatom
 
 option(COMPILER_RT_PROFILE_BAREMETAL "Build minimal baremetal profile library" OFF)
 
+set(DEFAULT_COMPILER_RT_BUILD_PROFILE_ROCM ON)
+if(APPLE)
+  set(DEFAULT_COMPILER_RT_BUILD_PROFILE_ROCM OFF)
+endif()
+option(COMPILER_RT_BUILD_PROFILE_ROCM
+  "Build the host-side ROCm/HIP device profile collection runtime"
+  ${DEFAULT_COMPILER_RT_BUILD_PROFILE_ROCM})
+mark_as_advanced(COMPILER_RT_BUILD_PROFILE_ROCM)
+
 include(config-ix)
 
 #================================
diff --git a/compiler-rt/lib/profile/CMakeLists.txt b/compiler-rt/lib/profile/CMakeLists.txt
index 36e6b5a6a21c1..b8745696bc6ce 100644
--- a/compiler-rt/lib/profile/CMakeLists.txt
+++ b/compiler-rt/lib/profile/CMakeLists.txt
@@ -89,11 +89,13 @@ if (NOT COMPILER_RT_PROFILE_BAREMETAL)
   list(APPEND PROFILE_SOURCES
     GCDAProfiling.c
     InstrProfilingFile.c
-    InstrProfilingPlatformROCm.cpp
     InstrProfilingRuntime.cpp
     InstrProfilingUtil.c
     InstrProfilingValue.c
     )
+  if(COMPILER_RT_BUILD_PROFILE_ROCM)
+    list(APPEND PROFILE_SOURCES InstrProfilingPlatformROCm.cpp)
+  endif()
 endif()
 
 set(PROFILE_HEADERS
@@ -156,6 +158,12 @@ if(COMPILER_RT_PROFILE_BAREMETAL)
      -DCOMPILER_RT_PROFILE_BAREMETAL=1)
 endif()
 
+if(COMPILER_RT_BUILD_PROFILE_ROCM)
+  set(EXTRA_FLAGS
+      ${EXTRA_FLAGS}
+      -DCOMPILER_RT_BUILD_PROFILE_ROCM=1)
+endif()
+
 set(PROFILE_OBJECT_LIBS)
 if(COMPILER_RT_HAS_INTERCEPTION AND NOT COMPILER_RT_PROFILE_BAREMETAL)
   # RTInterception references __sanitizer_internal_{memcpy,memset,memmove} and other
diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index 2200f07e70acc..9ea5a2638fac9 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -50,11 +50,13 @@
  * and the address test at the call site would fold to true.
  * Windows: __declspec(selectany) is data-only, and the ROCm interceptor path
  * is not used there, so keep the original strong extern. */
+#if COMPILER_RT_BUILD_PROFILE_ROCM
 #if defined(_WIN32)
 extern int __llvm_profile_hip_collect_device_data(void);
 #else
 __attribute__((weak)) int __llvm_profile_hip_collect_device_data(void);
 #endif
+#endif
 
 /* From where is profile name specified.
  * The order the enumerators define their
@@ -1217,11 +1219,13 @@ int __llvm_profile_write_file(void) {
    * InstrProfilingPlatformROCm.o is in the link, which happens when the program
    * references other ROCm-runtime symbols (HIP-with-PGO). Warning on failure is
    * handled inside the callee. */
+#if COMPILER_RT_BUILD_PROFILE_ROCM
 #if defined(_WIN32)
   (void)__llvm_profile_hip_collect_device_data();
 #else
   if (&__llvm_profile_hip_collect_device_data)
     (void)__llvm_profile_hip_collect_device_data();
+#endif
 #endif
 
   // Restore SIGKILL.

@yxsamliu yxsamliu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks

@guy-david
guy-david merged commit 2766733 into main May 28, 2026
13 checks passed
@guy-david
guy-david deleted the users/guy-david/profile-rocm-option branch May 28, 2026 13:55
zmodem added a commit that referenced this pull request Jun 3, 2026
…201416)

This broke profiling builds on Windows by switching the profile library
to link against the dynamic CRT; see discussion on the PR.

There were already a number of issues reported and fixed after this PR.
Rather than piling on the fixes (and this one may need some work),
revert back to green for now to let the project recover.

This reverts commit 5db1364.

Additionally, this reverts the followup PRs in
635e120,
2766733,
4c33844, and
5eca8b6:

"[PGO][HIP] Stop pulling ROCm.o into every PGO host link (#200101)"
"[compiler-rt][profile] Add COMPILER_RT_BUILD_PROFILE_ROCM option
(#200127)"
"[PGO][HIP] Skip ROCm interceptor in profile-only compiler-rt builds
(#200111)"
"[PGO][HIP] Fix profile-only Windows link by gating ROCm interceptor
macro (#200859)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler-rt PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants